#include "OpenglAppFramework/inc/Actions.h"
#include "OpenglAppFramework/inc/GameObject.h"
#include "OpenglAppFramework/inc/Scene.h"
#include "OpenglAppFramework/inc/OpenglEnv.h"
#include "OpenglAppFramework/inc/PlugIn/LightSource.h"
#include <iostream>

#define SUN_ANGLE_DAY_SPEED     (0.001) // pi*radis per frame
#define SUN_ANGLE_NIGHT_SPEED   (0.020) // pi*radis per frame
#define SUN_TRACK_R             (12)
#define PI                      (3.141592653589793238462643383279502884197169)

class SunActions : public Actions {
private:
    float m_angle = -0.5; // pi * radis
public:
    SunActions() {}
    ~SunActions() {}

    void routine(GameObject* obj) {
        if(m_angle >= 2) {
            m_angle = 0;
        }
        float x = glm::cos(m_angle * PI) * SUN_TRACK_R;
        float y = -glm::sin(m_angle * PI) * SUN_TRACK_R;
        float z = y * glm::cos(0.5);

        obj->setLocation(glm::vec3(x, y, z));
        LightSource* light = dynamic_cast<LightSource*>(obj->plugIns()["parallel_light_plugin"]);
        light->setDirectionNormalized(glm::normalize(glm::vec3(-x, -y, -z)));

        m_angle += (y >=  -0.1 * SUN_TRACK_R) ? SUN_ANGLE_DAY_SPEED : SUN_ANGLE_NIGHT_SPEED;
    }
};

extern "C" Actions* get_sun_actions() {
    return new SunActions();
}
